<?php

namespace apiend\modules\course\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\Url;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class GroupUserSearch extends \common\models\course\CourseGroupUsers {

    /**
     * Creates data provider instance with search query applied
     * @param array $id
     * @return ActiveDataProvider
     */
    public function search($id) {
        $query = static::find()
        ->alias('t')
        ->where([
            't.status' => static::STATUS_ACTIVE,
            't.group_id' => $id,
        ])
        ->with(['role', 'user'])
        ->orderBy([
            't.role_id' => SORT_DESC,
            't.created_at' => SORT_ASC,
        ]);
        $query->joinWith('user', true, 'INNER JOIN');


        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

//        return \common\helpers\Debug::sql($query);
        return $dataProvider;
    }

    public function fields() {
        return [
            'id' => 'user_id',
            'nickname' => function() {
                return $this->user->username;
            },
            'avatar' => function() {
                return Url::toRemote($this->user->avatar);
            },
            'role' => function() {
                return $this->role ? $this->role->name : '';
            },
        ];
    }

}
